<?php
define('IN_ECS', true);
require('../includes/init.php');
require('../includes/lib_order.php');
include_once('../includes/lib_payment.php');
error_reporting(E_ALL ^ E_NOTICE);
$out_trade_no = intval($_GET['out_trade_no']);

//根据支付id获取订单id
$order_id = $GLOBALS['db']->getOne("SELECT order_id FROM ".$GLOBALS['ecs']->table('pay_log')." WHERE log_id = '$out_trade_no'");

//获取订单信息
$order = $GLOBALS['db']->getRow("SELECT * FROM " . $GLOBALS['ecs']->table('order_info') . " WHERE order_id = '$order_id' OR parent_order_id = '$order_id' limit 1");

if($order)
{
	if ($order['order_amount'] > 0){
		//防止商户订单号重复
		$order['order_id'] = $out_trade_no.'-'.$order['order_amount']*100; 
		$payment = payment_info($order['pay_id']);
		include_once('../includes/modules/payment/' . $payment['pay_code'] . '.php');
		$pay_obj    = new $payment['pay_code'];
		$code = $pay_obj->get_code($order, unserialize_config($payment['pay_config']));
	}
	else
	{
		show_message('此订单已支付！'); 
	}
}
else
{
	echo 1;exit; 
}
?>
<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>微信安全支付</title>
	<script type="text/javascript">
		/*function jsApiCall()
		{
			WeixinJSBridge.invoke(
				'getBrandWCPayRequest',
				<?php echo $code;?>,
				function(res){
					//WeixinJSBridge.log(res.err_msg);
					if(res.err_msg == "get_brand_wcpay_request:ok" ) {
						window.location.href = "<?php echo return_url('weixin');?>";
					} else {
						alert("交易取消");
						window.location.href = "./index.php";
					}
				}
			);
		}
		//function callpay()
		window.onload = function ()

		{
			if (typeof WeixinJSBridge == "undefined"){
			    if( document.addEventListener ){
			        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
			    }else if (document.attachEvent){
			        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
			        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
			    }
			}else{
			    jsApiCall();
			}
		}*/
	</script>
</head>
<body>
<!--	</br></br></br></br>
	<div align="center">
		<button style="width:400px; height:100px; background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:28px;" type="button" onclick="callpay()" >微信支付</button>
	</div>
	-->
	</br></br></br></br>
	<div align="center">
		<button style="width:90%; height:50px; background-color:#FE6714; border:0px #FE6714 solid; cursor: pointer;  color:white;  font-size:28px;" type="button" onclick="wx_pay()" >APP微信支付</button>
	</div>
	<script>
	var wxPay;
  apiready = function(){
    wxPay = api.require('wxPay');
	wx_pay();
  };
  function wx_pay(){
	api.showProgress({
		style: 'default',
		animationType: 'fade',
		title: '在线充值中...',
		modal: false
	});
    wxPay.config( {
      apiKey: '',
      mchId: '',
      partnerKey: '',
      notifyUrl: 'http://'+document.domain+'/mobile/respond.php'
    }, function(ret, err){
      if(ret.status){
		var timestamp = new Date().getTime();
		var tradeNo='<?php echo $out_trade_no;?>-'+timestamp;
		api.hideProgress();
        wxPay.pay({
          description:	'在线支付',
          totalFee:		<?php echo $order[order_amount];?>*100,
          tradeNo:		tradeNo,
        },function(ret, err){
          if (ret.status) {
            api.toast({msg: '支付成功！'});
			window.history.go(-1); 
			//这里执行跳转
          } else {
            //alert(JSON.stringify(err)+tradeNo);
			if (err.code == '-2') {
				api.toast({msg: '用户取消'});
			} else {
				api.toast({msg: '支付失败！'});
			}
			window.history.go(-1); 
          }
        });
      }else{
		api.hideProgress();
		api.toast({msg: '充值失败！'});
		window.history.go(-1); 
        console.log(JSON.stringify(ret));
      }
    });
  }
 

</script>
</body>
</html>